package com.example.myapplication;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnmainclose;
    private Button btnmainsupplier;
    private Button btnMainbuy;
    private TextView tvMainresult;
    private  String [] parts=new String[]{"汽车引擎","轿车前玻璃","轿车方向盘"};
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainbuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog;
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("请选择原材料");
                builder.setMultiChoiceItems(parts, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       tvMainresult.setText(null);
                       for (int i=0;i<parts.length;i++){
                           if (listView.getCheckedItemPositions().get(i)){
                               tvMainresult.append(parts[i]+"\n");
                           }
                       }
                        dialog.dismiss();
                    }
                });
                dialog=builder.create();
                listView=dialog.getListView();
                dialog.show();
            }
        });
        btnmainsupplier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String [] supplies={"新星汽车配件","枫叶汽车配件","天辉汽车配件","西来汽车配件"};
                new AlertDialog.Builder(MainActivity.this).setTitle("请选中")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                            }
                        })
                        .setSingleChoiceItems(supplies, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                tvMainresult.setText(supplies[i]);
                            }
                        })
                        .create().show();
            }
        });
        btnmainclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog;
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("普通对话框");
                builder.setMessage("是否确定要退出");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.this.finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog=builder.create();
                dialog.show();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        btnmainclose = (Button) findViewById(R.id.btnmainclose);
        btnmainsupplier = (Button) findViewById(R.id.btnmainsupplier);
        btnMainbuy = (Button) findViewById(R.id.btnMainbuy);
        tvMainresult=(TextView)findViewById(R.id.tv_main_result);
    }
}